package com.jkhan.demo.threads;

/**
 * @ClassName TicketSell
 * @Description TicketSell
 * @Author hjk
 * @Date 2022/1/5 22:10
 **/
public class TicketSell1 extends Thread {
    public static void main(String[] args) {
        Windows1 windows1 = new Windows1();
        Windows1 windows2 = new Windows1();
        Windows1 windows3 = new Windows1();

        windows1.setName("windows1");
        windows2.setName("windows2");
        windows3.setName("windows3");

        windows1.start();
        windows2.start();
        windows3.start();
    }
}

class Windows1 extends Thread {
    private static int tickets = 100;
    private static Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName() + ": 卖票,票号为: " + tickets);
                    tickets--;
                } else {
                    break;
                }
            }
        }
    }
}
